const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
function InitTime(endtime){
    var dd,hh,mm,ss = null;
    var time = (new Date(endtime).getTime() - new Date().getTime())/1000;
    if(time<=0){
        return '结束'
    }else{
        dd = Math.floor(time / 60 / 60 / 24);
        hh = Math.floor((time / 60 / 60) % 24);
        mm = Math.floor((time / 60) % 60);
        ss = Math.floor(time  % 60);
        var str = dd+"天"+hh+":"+mm+":"+ss;
        return str;
    }
}
Page({
  data: {
    globalStype:'',
  	goodsId:'',
  	shopId:'',
  	selectByGoodsIddata:[],
    isloading:true,
    goodsdata:'',
    myInformationdata:'',
  },
  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    if(option.id){
      this.setData({
        goodsId:option.id
      })
      this.selectByGoodsId()
    }
    if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId
      })
    }
    this.setData({
    	myInformationdata:app.globalData.myshopinfo
    })
  },
  selectByGoodsId(){
    Controllers.selectByGoodsId({
      goodsId:this.data.goodsId
    }).then(res => {

      if(res.data.marketAccountList.length>0){
        this.setData({
          selectByGoodsIddata:res.data.marketAccountList,
          goodsdata:res.data,
        })

        for(let item of this.data.selectByGoodsIddata){
          if(item.endTime){
            var that=this;
            setInterval(()=> {
              item.downtime=InitTime(item.endTime.replace(/-/g, '/'))
              that.setData({
                selectByGoodsIddata:that.data.selectByGoodsIddata
              })
            }, 1000);
          }
        }

      
      }
      this.setData({
        isloading:false
      })

    });
  },
  gogroup(e){
    let pin=e.currentTarget.dataset.pin;
    if(pin==1){
      this.setData({
        isPin:true
      })
    }else{
      this.setData({
        isPin:false
      })
    }
    let goods={};
    goods=this.data.goodsdata
    goods.shopId=this.data.shopId
    goods.content=""
    goods.isPin=this.data.isPin
    goods.goodsId=this.data.goodsId
    goods.marketList=[]
    goods.marketList.push({pinPrice:""})
    goods.marketList[0].pinPrice=this.data.goodsdata.marketAccountList[0].pinPrice
    goods.num=1
    let marketNo=e.currentTarget.dataset.no
    let vip=goods.vip || 0
    this.goodsSettlement(goods,marketNo,vip)
  },
  goodsSettlement(goods,marketNo,vip){
    // console.log(JSON.stringify(goods));
    if(this.data.myInformationdata.vip==0 && vip==1){
        wx.showToast({
          title: '会员专属，请注册为会员！',
          icon: 'none'
        })
    }else{
      //  wx.showToast({
      //   title: '下单成功！',
      //   icon: 'none',
      //   duration: 1000
      // })
      setTimeout(res=>{
        wx.navigateTo({
          url:'/pages/cart/submitOrder/submitOrder?good='+ encodeURIComponent(JSON.stringify(goods)) +'&isGood=true&marketNo='+ marketNo
        },1000)
      })
      
    }
  },
})